<template>
  <v-dialog v-model="dialogShow" :transition="dialogConfigRedirect.transition">
    <v-card>
      <v-toolbar color="primary" dark>
        <label>{{ dialogConfigRedirect.title }}</label>
        <v-spacer></v-spacer>
        <v-btn icon @click="dialogShow = false">
          <v-icon color=" darken-2">mdi-close</v-icon>
        </v-btn>
      </v-toolbar>
      <v-card-text>
        <form-component :form-data="formData" :form-config="formConfig" @formCancel="formCancel" @formSave="formSave" />
      </v-card-text>
      <v-card-actions class="justify-end">
        <v-btn text @click="dialogShow = false">Close</v-btn>
      </v-card-actions>
    </v-card>
  </v-dialog>
</template>

<script>
import { assignObject } from '@/utils/tools'
import FormComponent from '../FormCompoent/FormComponent.vue'
export default {
  // 弹窗组件
  name: 'DialogComponent',

  components: { FormComponent },

  model: {
    prop: 'dialogConfig',
    event: 'setDialogConfig'
  },

  props: {
    // dialog配置
    // dialogConfig: { type: Object, default: () => {} },
    dialogConfig: { type: Object, default: () => ({}) },

    // 表单数据
    formData: { type: Object, default: () => {} },
    // 表单配置
    formConfig: { type: Object, default: () => {} }
  },

  data() {
    return {
      dialogConfigRedirect: this.dialogConfig,
      dialogShow: this.dialogConfig.show || false
    }
  },

  watch: {
    'dialogConfig.show': {
      handler(nv) {
        this.dialogShow = nv
      }
    },
    dialogShow(nv) {
      this.dialogConfigRedirect.show = nv
      this.$emit('setDialogConfig', this.dialogConfigRedirect)
    }
  },

  created() {
    this.setDialogDefaultAttributes()
  },

  methods: {
    setDialogDefaultAttributes() {
      const defaultAttributes = { transition: 'dialog-bottom-transition', show: false }
      this.dialogConfigRedirect = assignObject(defaultAttributes, this.dialogConfig)
      this.$emit('setDialogConfig', this.dialogConfigRedirect)
    },

    formCancel() {},

    formSave() {}
  }
}
</script>

<style></style>
